// packageA/pages/partsOrderDetail/partsOrderDetail.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
const util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    editInfo:null,
    //用户权限
    authority:null,
    //折叠面板
    activeLogistics: null,
    activePart: null,
    //当前登录用户
    user:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      user:app.globalData.userInfo,
      authority:app.globalData.authority
    })

    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('查看配件采购传参');
      console.log(paramObj);  

      if(paramObj.id){
        //获取配件采购信息
        this.getInfo(paramObj.id);
      }
    }  
  },

  //获取配件采购信息
  getInfo(id){
    let param = {
      isAuth: false,
      className: 'partsPurchaseOrder',
      data: {
        id: id,
        selectFields: ['id', 'cities', 'totalPurchasePrice', 'remark', 'server', 'staff', 'citiesName', 'freight','state', 'serverId', 'factoryName', 'address', 'purchasingTime', 'purchaseClassify', 'factoryId','operation', 'orderNo', 'contact', 'factory', 'reject', 'serverName', 'parts', 'logistics',
        'equipmentNo', 'salesperson', 'consignee', 'phone', 'clientDepart', 'shippedNum', 'appId', 'outStorageType','inStorageType', 'dataAuthority', 'totalSalePrice', 'project', 'orderPersonnel', 'purchaseParts','approvalProcess', 'currentApprovalMan', 'purchaseApproval','logisticsParts']
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result[0];
        console.log('采购订单回显');
        console.log(result);

        //采购时间
        if(result.purchasingTime){
          result.purchasingTime=util.timeFormat(result.purchasingTime);
        }

        //审批流
        if(result.approvalProcess && result.approvalProcess.approvalProcess && result.approvalProcess.approvalProcess.length){
          let arr=result.approvalProcess.approvalProcess;
          let str='';
          arr.forEach((item,index)=> {
            str += (index+1) + ' ' + item.name + ' ';
          });
          result.approvalProcess.self_name=str;
        }
        
        if(result.logisticsParts && result.logisticsParts.length){
          //时间转化
          result.logisticsParts.forEach(item => {
            if(item.deliverTime){
              item.deliverTime=util.timeFormat(item.deliverTime);
            }
          });

          //只有一条物流则展开
          if(result.logisticsParts.length==1){
            this.setData({
              activeLogistics: (result.logisticsParts)[0].id
            })
          }
        }        

        //配件中设备型号
        if(result.purchaseParts && result.purchaseParts.length){
          result.purchaseParts.forEach(item => {
            let arrs=[];
            if(item.part.brands && item.part.brands.length){
              let brands=item.part.brands;
              brands.forEach(item2 => {
                arrs.push(item2.name)
              });
            }
            item.self_brands=arrs;
          });
        }
        
        this.setData({
          editInfo: result
        })
      }
    }).catch((error) => {
    })
  },

  //切换物流折叠面板
  onChangeLogistics(event) {
    this.setData({
      activeLogistics: event.detail
    })
  },

  //切换配件折叠面板
  onChangePart(event) {
    this.setData({
      activePart: event.detail
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})